﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OBiCMSFramework;
using System.Data;
using System.Xml;
using System.IO;
using System.Web;
using System.Diagnostics;

namespace OBiCMSUI
{
    public class SiteManager : WidgetBase
    {
        private enum SaveType
        {
            WBST
        }

        // Public Constructors
        public SiteManager(String ID) 
            : base(ID)
        {

        }

        // Public Methods
        public override List<String> DefaultSettings()
        {
            return new List<String>();
        }
        public override StringBuilder PostBack(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String sql = "";

            if (context.Request("postBackType") == "template")
            {
                String templateID = "";
                String templateSettingsID = "";
                String templateSettingsIDC = "";
                
                if (context.Request("templateID") != "")
                {
                    templateID = context.Request("templateID");
                    templateSettingsID = context.Request("settingsID");

                    sql = "Update CMSTemplate Set Name = '" + Utilities.ReplaceForDB(context.Request("templateName")) + "' Where CMSTemplateID = '" + templateID + "';" +
                          "Update CMSTemplateSettings Set Body = '" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("htmlBody"))) + "', Javascript = '" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("jsBody"))) + "' Where CMSTemplateSettingsID = '" + templateSettingsID + "';";

                    Utilities.UpdateDatabase(sql);

                    sql = "SELECT CMSTemplateSettings.CMSTemplateSettingsIDC " +
                          "FROM CMSTemplate CROSS JOIN CMSTemplateSettings " +
                          "Where CMSTemplate.CMSTemplateID = '" + templateID + "' AND CMSTemplateSettings.CMSTemplateSettingsID = '" + templateSettingsID + "'";

                    DataSet templateIDC = Utilities.GetDataset(sql);

                    templateSettingsIDC = templateIDC.Tables[0].Rows[0]["CMSTemplateSettingsIDC"].ToString();
                }
                else
                {
                    templateID = Utilities.GenerateID("CMSTMP");
                    templateSettingsID = Utilities.GenerateID("CMSTMPS");

                    sql = "INSERT INTO [OBiCMS].[dbo].[CMSTemplate] ([CMSTemplateID],[Name]) VALUES ('" + templateID + "', '" + Utilities.ReplaceForDB(context.Request("templateName")) + "');" +
                          "INSERT INTO [OBiCMS].[dbo].[CMSTemplateSettings] ([CMSTemplateSettingsID],[Version],[Body],[Javascript],[Online]) " +
                          "VALUES ('" + templateSettingsID + "', 1,'" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("htmlBody"))) + "','" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("jsBody"))) + "',1);";

                    Utilities.UpdateDatabase(sql);

                    sql = "SELECT CMSTemplate.CMSTemplateIDC, CMSTemplateSettings.CMSTemplateSettingsIDC " +
                          "FROM CMSTemplate CROSS JOIN CMSTemplateSettings " +
                          "Where CMSTemplate.CMSTemplateID = '" + templateID + "' AND CMSTemplateSettings.CMSTemplateSettingsID = '" + templateSettingsID + "'";

                    DataSet templateIDs = Utilities.GetDataset(sql);

                    templateSettingsIDC = templateIDs.Tables[0].Rows[0]["CMSTemplateSettingsIDC"].ToString();

                    sql = "INSERT INTO [OBiCMS].[dbo].[LK_CMSTemplate_CMSTemplateSettings] ([LK_CMSTemplate_CMSTemplateSettingsID],[CMSTemplateIDC],[CMSTemplateSettingsIDC]) " +
                          "VALUES ('" + Utilities.GenerateID("LKTMPTMPS") + "'," + templateIDs.Tables[0].Rows[0]["CMSTemplateIDC"] + ", " + templateIDs.Tables[0].Rows[0]["CMSTemplateSettingsIDC"] + ")";

                    Utilities.UpdateDatabase(sql);
                }

                if (context.Request("cssID") != "")
                {
                    sql = "Update [CMSCSSSnippet] Set Name = '" + Utilities.ReplaceForDB(context.Request("cssName")) + "' Where [CMSCSSSnippetID] = '" + context.Request("cssID") + "';" +
                          "Update [CMSCSSSnippetSettings] Set Body = '" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("cssBody"))) + "' Where CMSCSSSnippetSettingsID = '" + context.Request("cssSettingsID") + "';";

                    Utilities.UpdateDatabase(sql);
                }
                else
                {
                    String cssID = Utilities.GenerateID("CMSCSS");
                    String cssSettingsID = Utilities.GenerateID("CMSCSSS");

                    sql = "INSERT INTO [OBiCMS].[dbo].[CMSCSSSnippet] ([CMSCSSSnippetID],[Name]) VALUES ('" + cssID + "', '" + Utilities.ReplaceForDB(context.Request("cssName")) + "');" +
                          "INSERT INTO [OBiCMS].[dbo].[CMSCSSSnippetSettings] ([CMSCSSSnippetSettingsID],[Version],[Body],[Type],[Online]) " +
                          "VALUES ('" + cssSettingsID + "', 1,'" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("cssBody"))) + "',7,1);";

                    Utilities.UpdateDatabase(sql);

                    sql = "SELECT CMSCSSSnippet.CMSCSSSnippetIDC, CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC " +
                          "FROM CMSCSSSnippet CROSS JOIN CMSCSSSnippetSettings " +
                          "Where CMSCSSSnippet.CMSCSSSnippetID = '" + cssID + "' AND CMSCSSSnippetSettings.CMSCSSSnippetSettingsID = '" + cssSettingsID + "'";

                    DataSet cssIDs = Utilities.GetDataset(sql);

                    sql = "INSERT INTO [OBiCMS].[dbo].[LK_CMSCSSSnippet_CMSCSSSnippetSettings] ([LK_CMSCSSSnippet_CMSCSSSnippetSettingsID],[CMSCSSSnippetIDC],[CMSCSSSnippetSettingsIDC]) " +
                          "VALUES ('" + Utilities.GenerateID("LKCSSCSSS") + "'," + cssIDs.Tables[0].Rows[0]["CMSCSSSnippetIDC"] + ", " + cssIDs.Tables[0].Rows[0]["CMSCSSSnippetSettingsIDC"] + ");" +
                          "INSERT INTO [OBiCMS].[dbo].[LK_CMSTemplateSettings_CMSCSSSnippet] ([LK_CMSTemplateSettings_CMSCSSSnippetID],[CMSTemplateSettingstIDC],[CMSCSSSnippetIDC],[CMSCSSSnippetLoadOrder]) " +
                          "VALUES ('" + Utilities.GenerateID("LKTMPSCSS") + "'," + templateSettingsIDC + ", " + cssIDs.Tables[0].Rows[0]["CMSCSSSnippetIDC"] + ", 1);";

                    Utilities.UpdateDatabase(sql);
                }
            }

            return LoadTemplates(context, pageSettings, widgetSettings, timer, useDiagnostics);
        }
        public override StringBuilder GenerateWidget(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            if (context.Request("job") != "")
                return AjaxCall(context, pageSettings, widgetSettings, timer, useDiagnostics);

            StringBuilder returnBody = new StringBuilder("");
            XmlReader widgetSettingsReader = XmlReader.Create(new StringReader(widgetSettings));

            while (widgetSettingsReader.Read())
            {
                if (widgetSettingsReader.NodeType == XmlNodeType.Element)
                {
                    switch (widgetSettingsReader.Name.ToLower())
                    {
                        case "sitepagemanager":
                            returnBody.Append(Template.getTemplate(widgetSettingsReader.GetAttribute("id")).GenerateTemplate(context, pageSettings, widgetSettings, timer, useDiagnostics).ToString());
                            //returnBody.Append(LoadWebSites(widgetSettingsReader.GetAttribute("id"), context, pageSettings, widgetSettings, timer, useDiagnostics));
                            break;
                        case "templatemanager":
                            returnBody.Append(LoadTemplates(context, pageSettings, widgetSettings, timer, useDiagnostics));
                            break;
                    }
                }
            }

            return returnBody;
        }

        // Private Methods
        private StringBuilder AjaxCall(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            StringBuilder returnStr = new StringBuilder("<cmsManager>" + Environment.NewLine +
                                                        "<functionID>" + context.Request("Rndd") + "</functionID>" + Environment.NewLine +
                                                        "<JSON><![CDATA[");
            switch (context.Request("job"))
            {
                case "add-website":
                    returnStr.Append(AddNewWebsite(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-basepage":
                    returnStr.Append(GetPageDetails(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-basepage-versions":
                    returnStr.Append(GetPageVersions(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-csssnippet-versions":
                    returnStr.Append(GetCssSnippetVersions(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-csssnippet":
                    returnStr.Append(GetCssSnippetDetails(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-docTypes":
                    returnStr.Append(GetDocTypes(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-template":
                    returnStr.Append(GetTemplateDetails(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "load-template-versions":
                    returnStr.Append(GetTemplateVersions(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "save":
                    returnStr.Append(CMSSave(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
                case "set-preview":
                    context.SetSession("$$CMSPREVIEWSETTINGS$$", context.Request("settingsXML"));
                    break;
                case "setup-manager":
                    returnStr.Append(SetupManager(context, pageSettings, widgetSettings, timer, useDiagnostics));
                    break;
            }

            returnStr.Append("]]></JSON>" + Environment.NewLine +
                             "</cmsManager>");

            return returnStr;
        }

        private String CMSSave(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            switch (context.Request("type"))
            {
                case "basepage":
                    return SaveBasePage(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "basepage-live-version":
                    return SaveBasePageVersionLive(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "basepage-version-details":
                    return SaveBasePageVersionDetail(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "csssnippet":
                    return SaveCssSnippet(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "csssnippet-live-version":
                    return SaveCssSnippetVersionLive(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "csssnippet-version-details":
                    return SaveCssSnippetVersionDetail(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "template":
                    return SaveTemplate(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "template-live-version":
                    return SaveTemplateVersionLive(context, pageSettings, widgetSettings, timer, useDiagnostics);
                case "template-version-details":
                    return SaveTemplateVersionDetail(context, pageSettings, widgetSettings, timer, useDiagnostics);
                default :
                    return "";
            }            
        }

        private String SaveBasePage(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String sql = "UPDATE CMSBasePage " +
                         "SET Name = '" + Utilities.ReplaceForDB(context.Request("name")) + "', " +
                         "Online = '" + Utilities.ReplaceForDB(context.Request("online")) + "' " +
                         "WHERE CMSBasePageID = '" + Utilities.ReplaceForDB(context.Request("ID")) + "'";

            Utilities.UpdateDatabase(sql);

            return "";
        }
        private String SaveBasePageVersionLive(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            SetBasePageVersion(context.Request("ID"), context.Request("liveVersion"));

            return "{\"liveVersion\" : \"" + context.Request("liveVersion") + "\"}";
        }
        private String SaveBasePageVersionDetail(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String returnStr = "";
            String sql = "exec dbo.createBasePageVersion " +
                         "'" + Utilities.ReplaceForDB(context.Request("ID")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("docType")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("title")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("metaDesc")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("metaKeywords")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("favIcon")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("shortIcon")) + "', " +
                         "'" + Utilities.ReplaceForDB(context.Request("settingsXML").Replace("[CMSLTS]", "<").Replace("[CMSQM]", "?")) + "'";

            DataSet ds = Utilities.GetDataset(sql);

            if (ds.Tables[0].Rows.Count > 0)
            {
                returnStr += "{\"versions\" : [" + Environment.NewLine;
                returnStr += "{" + Environment.NewLine;
                returnStr += "\"version\" : \"" + ds.Tables[0].Rows[0]["Version"].ToString() + "\"," + Environment.NewLine;
                if (context.Request("online") == "1")
                {
                    SetBasePageVersion(context.Request("ID"), ds.Tables[0].Rows[0]["Version"].ToString());
                    returnStr += "\"online\" : \"True\"" + Environment.NewLine;
                }
                else
                    returnStr += "\"online\" : \"False\"" + Environment.NewLine;

                returnStr += "}" + Environment.NewLine;
                returnStr += "]}" + Environment.NewLine;


            }

            return returnStr;
        }
        private void SetBasePageVersion(String ID, String versionNo)
        {
            String sql = "UPDATE CMSBasePageSettings " +
                         "SET Online = 0 " +
                         "FROM CMSBasePage INNER JOIN " +
                         "LK_CMSBasePage_CMSBasePageSettings ON CMSBasePage.CMSBasePageIDC = LK_CMSBasePage_CMSBasePageSettings.CMSBasePageIDC INNER JOIN " +
                         "CMSBasePageSettings ON LK_CMSBasePage_CMSBasePageSettings.CMSBasePageSettingsIDC = CMSBasePageSettings.CMSBasePageSettingsIDC " +
                         "WHERE CMSBasePage.CMSBasePageID = '" + Utilities.ReplaceForDB(ID) + "'";

            Utilities.UpdateDatabase(sql);

            sql = "UPDATE CMSBasePageSettings " +
                  "SET Online = 1 " +
                  "FROM CMSBasePage INNER JOIN " +
                  "LK_CMSBasePage_CMSBasePageSettings ON CMSBasePage.CMSBasePageIDC = LK_CMSBasePage_CMSBasePageSettings.CMSBasePageIDC INNER JOIN " +
                  "CMSBasePageSettings ON LK_CMSBasePage_CMSBasePageSettings.CMSBasePageSettingsIDC = CMSBasePageSettings.CMSBasePageSettingsIDC " +
                  "WHERE CMSBasePage.CMSBasePageID = '" + Utilities.ReplaceForDB(ID) + "' AND CMSBasePageSettings.Version = '" + Utilities.ReplaceForDB(versionNo) + "'";

            Utilities.UpdateDatabase(sql);
        }
        private String SaveCssSnippet(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String sql = "UPDATE CMSCSSSnippet " +
                         "SET Name = '" + Utilities.ReplaceForDB(context.Request("name")) + "' " +
                         "WHERE CMSCSSSnippetID = '" + Utilities.ReplaceForDB(context.Request("ID")) + "'";

            Utilities.UpdateDatabase(sql);

            return "";
        }
        private String SaveCssSnippetVersionLive(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            SetCssSnippetVersion(context.Request("ID"), context.Request("liveVersion"));
            return "{\"liveVersion\" : \"" + context.Request("liveVersion") + "\"}";
        }
        private String SaveCssSnippetVersionDetail(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String returnStr = "";
            String sql = "exec dbo.createCssSnippetVersion " +
                         "'" + Utilities.ReplaceForDB(context.Request("ID")) + "', " +
                         "'" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("csssnippet"))) + "'";

            DataSet ds = Utilities.GetDataset(sql);

            if (ds.Tables[0].Rows.Count > 0)
            {
                returnStr += "{\"versions\" : [" + Environment.NewLine;
                returnStr += "{" + Environment.NewLine;
                returnStr += "\"version\" : \"" + ds.Tables[0].Rows[0]["Version"].ToString() + "\"," + Environment.NewLine;
                if (context.Request("online") == "1")
                {
                    SetCssSnippetVersion(context.Request("ID"), ds.Tables[0].Rows[0]["Version"].ToString());
                    returnStr += "\"online\" : \"True\"" + Environment.NewLine;
                }
                else
                    returnStr += "\"online\" : \"False\"" + Environment.NewLine;

                returnStr += "}" + Environment.NewLine;
                returnStr += "]}" + Environment.NewLine;
            }

            return returnStr;
        }
        private void SetCssSnippetVersion(String ID, String versionNo)
        {
            String sql = "UPDATE CMSCSSSnippetSettings " +
                         "SET Online = 0 " +
                         "FROM CMSCSSSnippet INNER JOIN " +
                         "LK_CMSCSSSnippet_CMSCSSSnippetSettings ON CMSCSSSnippet.CMSCSSSnippetIDC = LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetIDC INNER JOIN " +
                         "CMSCSSSnippetSettings ON LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC = CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC " +
                         "WHERE CMSCSSSnippet.CMSCSSSnippetID = '" + Utilities.ReplaceForDB(ID) + "'";

            Utilities.UpdateDatabase(sql);

            sql = "UPDATE CMSCSSSnippetSettings " +
                  "SET Online = 1 " +
                  "FROM CMSCSSSnippet INNER JOIN " +
                  "LK_CMSCSSSnippet_CMSCSSSnippetSettings ON CMSCSSSnippet.CMSCSSSnippetIDC = LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetIDC INNER JOIN " +
                  "CMSCSSSnippetSettings ON LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC = CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC " +
                  "WHERE CMSCSSSnippet.CMSCSSSnippetID = '" + Utilities.ReplaceForDB(ID) + "' AND CMSCSSSnippetSettings.Version = '" + Utilities.ReplaceForDB(versionNo) + "'";

            Utilities.UpdateDatabase(sql);
        }
        private String SaveTemplate(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String sql = "UPDATE CMSTemplate " +
                         "SET Name = '" + Utilities.ReplaceForDB(context.Request("name")) + "' " +
                         "WHERE CMSTemplateID = '" + Utilities.ReplaceForDB(context.Request("ID")) + "'";

            Utilities.UpdateDatabase(sql);

            return "";
        }
        private String SaveTemplateVersionLive(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            SetTemplateVersion(context.Request("ID"), context.Request("liveVersion"));
            return "{\"liveVersion\" : \"" + context.Request("liveVersion") + "\"}";
        }
        private String SaveTemplateVersionDetail(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String returnStr = "";
            String sql = "exec dbo.createTemplateVersion " +
                         "'" + Utilities.ReplaceForDB(context.Request("ID")) + "', " +
                         "'" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("template"))) + "', " +
                         "'" + Utilities.ReplaceForDB(Utilities.ReplaceAllowedCharacters(context.Request("javascript"))) + "'";
            
            DataSet ds = Utilities.GetDataset(sql);

            if (ds.Tables[0].Rows.Count > 0)
            {
                returnStr += "{\"versions\" : [" + Environment.NewLine;
                returnStr += "{" + Environment.NewLine;
                returnStr += "\"version\" : \"" + ds.Tables[0].Rows[0]["Version"].ToString() + "\"," + Environment.NewLine;
                if (context.Request("online") == "1")
                {
                    SetTemplateVersion(context.Request("ID"), ds.Tables[0].Rows[0]["Version"].ToString());
                    returnStr += "\"online\" : \"True\"" + Environment.NewLine;
                }
                else
                    returnStr += "\"online\" : \"False\"" + Environment.NewLine;

                returnStr += "}" + Environment.NewLine;
                returnStr += "]}" + Environment.NewLine;
            }

            return returnStr;
        }
        private void SetTemplateVersion(String ID, String versionNo)
        {
            String sql = "UPDATE CMSTemplateSettings " +
                         "SET Online = 0 " +
                         "FROM CMSTemplate INNER JOIN " +
                         "LK_CMSTemplate_CMSTemplateSettings ON CMSTemplate.CMSTemplateIDC = LK_CMSTemplate_CMSTemplateSettings.CMSTemplateIDC INNER JOIN " +
                         "CMSTemplateSettings ON LK_CMSTemplate_CMSTemplateSettings.CMSTemplateSettingsIDC = CMSTemplateSettings.CMSTemplateSettingsIDC " +
                         "WHERE CMSTemplate.CMSTemplateID = '" + Utilities.ReplaceForDB(ID) + "'";

            Utilities.UpdateDatabase(sql);

            sql = "UPDATE CMSTemplateSettings " +
                  "SET Online = 1 " +
                  "FROM CMSTemplate INNER JOIN " +
                  "LK_CMSTemplate_CMSTemplateSettings ON CMSTemplate.CMSTemplateIDC = LK_CMSTemplate_CMSTemplateSettings.CMSTemplateIDC INNER JOIN " +
                  "CMSTemplateSettings ON LK_CMSTemplate_CMSTemplateSettings.CMSTemplateSettingsIDC = CMSTemplateSettings.CMSTemplateSettingsIDC " +
                  "WHERE CMSTemplate.CMSTemplateID = '" + Utilities.ReplaceForDB(ID) + "' AND CMSTemplateSettings.Version = '" + Utilities.ReplaceForDB(versionNo) + "'";

            Utilities.UpdateDatabase(sql);
        }

        private String GetAllTemplates(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            StringBuilder returnStr = new StringBuilder("{" + Environment.NewLine +
                                        "\"type\" : \"folder\"," + Environment.NewLine +
                                        "\"id\" : \"GlobalTemplates\"," + Environment.NewLine +
                                        "\"name\" : \"Templates\"," + Environment.NewLine);
            String sql = "SELECT CMSTemplateID, Name " +
                         "FROM CMSTemplate";

            DataSet templateDS = Utilities.GetDataset(sql);

            returnStr.Append("\"node\" : [");
            foreach (DataRow dr in templateDS.Tables[0].Rows)
            {
                returnStr.Append("{" + Environment.NewLine);
                returnStr.Append("\"type\" : \"template\"," + Environment.NewLine);
                returnStr.Append("\"id\" : \"" + dr["CMSTemplateID"].ToString() + "\"," + Environment.NewLine);
                returnStr.Append("\"name\" : \"" + dr["Name"].ToString() + "\"" + Environment.NewLine);
                returnStr.Append("},");
            }

            if (templateDS.Tables[0].Rows.Count > 0)
                returnStr.Remove(returnStr.Length - 1, 1);

            returnStr.Append("]}");
            return returnStr.ToString();
        }
        private String GetAllCss(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            StringBuilder returnStr = new StringBuilder("{" + Environment.NewLine +
                                        "\"type\" : \"folder\"," + Environment.NewLine +
                                        "\"id\" : \"GlobalCss\"," + Environment.NewLine +
                                        "\"name\" : \"Css Snippets\"," + Environment.NewLine);

            String sql = "SELECT CMSCSSSnippetID, Name " +
                         "FROM CMSCSSSnippet";

            DataSet cssSnippetDS = Utilities.GetDataset(sql);

            returnStr.Append("\"node\" : [");
            foreach (DataRow dr in cssSnippetDS.Tables[0].Rows)
            {
                returnStr.Append("{" + Environment.NewLine);
                returnStr.Append("\"type\" : \"csssnippet\"," + Environment.NewLine);
                returnStr.Append("\"id\" : \"" + dr["CMSCSSSnippetID"].ToString() + "\"," + Environment.NewLine);
                returnStr.Append("\"name\" : \"" + dr["Name"].ToString() + "\"" + Environment.NewLine);
                returnStr.Append("},");
            }

            if (cssSnippetDS.Tables[0].Rows.Count > 0)
                returnStr.Remove(returnStr.Length - 1, 1);

            returnStr.Append("]}");
            return returnStr.ToString();
        }

        private String GetPageVersions(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String basePageID = context.Request("ID");

            String sql = "SELECT Name, Online " +
                         "FROM CMSBasePage " +
                         "Where CMSBasePageID = '" + basePageID + "' ";

            DataSet basePageDS = Utilities.GetDataset(sql);

            StringBuilder returnStr = new StringBuilder("{\"name\" : \"" + basePageDS.Tables[0].Rows[0]["Name"].ToString() + "\"," + Environment.NewLine);
            returnStr.Append("\"online\" : \"" + basePageDS.Tables[0].Rows[0]["Online"].ToString() + "\"," + Environment.NewLine);

            returnStr.Append("\"versions\" : [" + Environment.NewLine);
            sql = "SELECT CMSBasePageSettings.Version, CMSBasePageSettings.Online " +
                         "FROM CMSBasePage INNER JOIN " +
                         "LK_CMSBasePage_CMSBasePageSettings ON " +
                         "CMSBasePage.CMSBasePageIDC = LK_CMSBasePage_CMSBasePageSettings.CMSBasePageIDC INNER JOIN " +
                         "CMSBasePageSettings ON LK_CMSBasePage_CMSBasePageSettings.CMSBasePageSettingsIDC = CMSBasePageSettings.CMSBasePageSettingsIDC " +
                         "Where CMSBasePage.CMSBasePageID = '" + basePageID + "' " +
                         "Order By CMSBasePageSettings.Version";

            basePageDS.Clear();
            basePageDS = Utilities.GetDataset(sql);

            foreach (DataRow dr in basePageDS.Tables[0].Rows)
            {
                returnStr.Append("{" + Environment.NewLine);
                returnStr.Append("\"version\" : \"" + dr["Version"].ToString() + "\"," + Environment.NewLine);
                returnStr.Append("\"online\" : \"" + dr["online"].ToString() + "\"" + Environment.NewLine);
                returnStr.Append("},");
            }

            if (basePageDS.Tables[0].Rows.Count > 0)
                returnStr.Remove(returnStr.Length - 1, 1);

            returnStr.Append("]}");

            return returnStr.ToString();
        }
        private String GetTemplateVersions(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String templateID = context.Request("ID");
            String sql = "SELECT Name " +
                         "FROM CMSTemplate " +
                         "Where CMSTemplateID = '" + templateID + "' ";

            DataSet basePageDS = Utilities.GetDataset(sql);

            StringBuilder returnStr = new StringBuilder("{\"name\" : \"" + basePageDS.Tables[0].Rows[0]["Name"].ToString() + "\"," + Environment.NewLine);

            returnStr.Append("\"versions\" : [" + Environment.NewLine);

            sql = "SELECT CMSTemplateSettings.Version, CMSTemplateSettings.Online " +
                         "FROM CMSTemplate INNER JOIN " +
                         "LK_CMSTemplate_CMSTemplateSettings ON CMSTemplate.CMSTemplateIDC = LK_CMSTemplate_CMSTemplateSettings.CMSTemplateIDC INNER JOIN " +
                         "CMSTemplateSettings ON LK_CMSTemplate_CMSTemplateSettings.CMSTemplateSettingsIDC = CMSTemplateSettings.CMSTemplateSettingsIDC " +
                         "Where CMSTemplate.CMSTemplateID = '" + templateID + "' " +
                         "Order By CMSTemplateSettings.Version";

            DataSet templateDS = Utilities.GetDataset(sql);

            foreach (DataRow dr in templateDS.Tables[0].Rows)
            {
                returnStr.Append("{" + Environment.NewLine);
                returnStr.Append("\"version\" : \"" + dr["Version"].ToString() + "\"," + Environment.NewLine);
                returnStr.Append("\"online\" : \"" + dr["online"].ToString() + "\"" + Environment.NewLine);
                returnStr.Append("},");
            }

            if (templateDS.Tables[0].Rows.Count > 0)
                returnStr.Remove(returnStr.Length - 1, 1);

            returnStr.Append("]}");

            return returnStr.ToString();
        }
        private String GetCssSnippetVersions(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String cssSnippetID = context.Request("ID");
            String sql = "SELECT Name " +
                         "FROM CMSCSSSnippet " +
                         "Where CMSCSSSnippetID = '" + cssSnippetID + "' ";

            DataSet basePageDS = Utilities.GetDataset(sql);

            StringBuilder returnStr = new StringBuilder("{\"name\" : \"" + basePageDS.Tables[0].Rows[0]["Name"].ToString() + "\"," + Environment.NewLine);

            returnStr.Append("\"versions\" : [" + Environment.NewLine);

            sql = "SELECT CMSCSSSnippetSettings.Version, CMSCSSSnippetSettings.Online " +
                         "FROM CMSCSSSnippet INNER JOIN " +
                         "LK_CMSCSSSnippet_CMSCSSSnippetSettings ON " +
                         "CMSCSSSnippet.CMSCSSSnippetIDC = LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetIDC INNER JOIN " +
                         "CMSCSSSnippetSettings ON LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC = CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC " +
                         "Where CMSCSSSnippet.CMSCSSSnippetID = '" + cssSnippetID + "' " +
                         "Order By CMSCSSSnippetSettings.Version";

            DataSet cssSnippetDS = Utilities.GetDataset(sql);

            foreach (DataRow dr in cssSnippetDS.Tables[0].Rows)
            {
                returnStr.Append("{" + Environment.NewLine);
                returnStr.Append("\"version\" : \"" + dr["Version"].ToString() + "\"," + Environment.NewLine);
                returnStr.Append("\"online\" : \"" + dr["online"].ToString() + "\"" + Environment.NewLine);
                returnStr.Append("},");
            }

            if (cssSnippetDS.Tables[0].Rows.Count > 0)
                returnStr.Remove(returnStr.Length - 1, 1);

            returnStr.Append("]}");

            return returnStr.ToString();
        }

        private String GetPageDetails(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String basePageID = context.Request("ID");
            String version = context.Request("versionNo");
            String returnStr = "{\"basePage\" : {" + Environment.NewLine;

            String sql = "SELECT CMSBasePage.CMSBasePageID, CMSBasePageSettings.Version, CMSBasePage.Name, CMSBasePageSettings.doctype, CMSBasePageSettings.Title, CMSBasePageSettings.MetaDescription, " +
                         "CMSBasePageSettings.MetaKeyWords, CMSBasePageSettings.FavIcon, CMSBasePageSettings.ShortcutIcon, " +
                         "CMSBasePageSettings.SettingsXML " +
                         "FROM CMSBasePage INNER JOIN " +
                         "LK_CMSBasePage_CMSBasePageSettings ON " +
                         "CMSBasePage.CMSBasePageIDC = LK_CMSBasePage_CMSBasePageSettings.CMSBasePageIDC INNER JOIN " +
                         "CMSBasePageSettings ON LK_CMSBasePage_CMSBasePageSettings.CMSBasePageSettingsIDC = CMSBasePageSettings.CMSBasePageSettingsIDC " +
                         "Where CMSBasePage.CMSBasePageID = '" + basePageID + "' AND CMSBasePageSettings.Version = '" + version + "' ";

            DataSet basePageDS = Utilities.GetDataset(sql);

            foreach (DataRow dr in basePageDS.Tables[0].Rows)
            {
                returnStr += "\"docType\" : \"" + dr["doctype"].ToString() + "\"," + Environment.NewLine;
                returnStr += "\"title\" : \"" + dr["Title"].ToString() + "\"," + Environment.NewLine;
                returnStr += "\"metaDesc\" : \"" + dr["MetaDescription"].ToString() + "\"," + Environment.NewLine;
                returnStr += "\"metaKeyWords\" : \"" + dr["MetaKeyWords"].ToString() + "\"," + Environment.NewLine;
                returnStr += "\"favIcon\" : \"" + dr["FavIcon"].ToString() + "\"," + Environment.NewLine;
                returnStr += "\"shortcutIcon\" : \"" + dr["ShortcutIcon"].ToString() + "\"," + Environment.NewLine;
                returnStr += "\"settingsXML\" : [ " + FormatSettingsToJSON(dr["SettingsXML"].ToString()) + " ]" + Environment.NewLine;
            }

            returnStr += "}}";

            return returnStr;
        }
        private String GetTemplateDetails(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String templateID = context.Request("ID");
            String version = context.Request("versionNo");
            String returnStr = "{\"template\" : {" + Environment.NewLine;

            String sql = "SELECT CMSTemplate.Name, CMSTemplateSettings.Version, CMSTemplateSettings.Body, CMSTemplateSettings.Javascript " +
                         "FROM CMSTemplate INNER JOIN " +
                         "LK_CMSTemplate_CMSTemplateSettings ON CMSTemplate.CMSTemplateIDC = LK_CMSTemplate_CMSTemplateSettings.CMSTemplateIDC INNER JOIN " +
                         "CMSTemplateSettings ON " +
                         "LK_CMSTemplate_CMSTemplateSettings.CMSTemplateSettingsIDC = CMSTemplateSettings.CMSTemplateSettingsIDC " +
                         "Where CMSTemplate.CMSTemplateID = '" + templateID + "' AND CMSTemplateSettings.Version = '" + version + "' ";

            DataSet templateDS = Utilities.GetDataset(sql);

            foreach (DataRow dr in templateDS.Tables[0].Rows)
            {
                returnStr += "\"template\" : \"" + context.HtmlEncode(Utilities.ReplaceForJSON(dr["Body"].ToString())) + "\"," + Environment.NewLine;
                returnStr += "\"javascript\" : \"" + context.HtmlEncode(Utilities.ReplaceForJSON(dr["Javascript"].ToString())) + "\"" + Environment.NewLine;
            }

            returnStr += "}}";

            return returnStr;
        }
        private String GetCssSnippetDetails(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String cssSnippetID = context.Request("ID");
            String version = context.Request("versionNo");
            String returnStr = "{\"csssnippet\" : {" + Environment.NewLine;

            String sql = "SELECT CMSCSSSnippet.CMSCSSSnippetID, CMSCSSSnippet.Name, CMSCSSSnippetSettings.Version, CMSCSSSnippetSettings.Body, CMSCSSSnippetSettings.Type " +
                         "FROM CMSCSSSnippet INNER JOIN " +
                         "LK_CMSCSSSnippet_CMSCSSSnippetSettings ON " +
                         "CMSCSSSnippet.CMSCSSSnippetIDC = LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetIDC INNER JOIN " +
                         "CMSCSSSnippetSettings ON " +
                         "LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC = CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC " +
                         "Where CMSCSSSnippet.CMSCSSSnippetID = '" + cssSnippetID + "' AND CMSCSSSnippetSettings.Version = '" + version + "' ";

            DataSet templateDS = Utilities.GetDataset(sql);

            foreach (DataRow dr in templateDS.Tables[0].Rows)
            {
                returnStr += "\"css\" : \"" + context.HtmlEncode(Utilities.ReplaceForJSON(dr["Body"].ToString())) + "\"" + Environment.NewLine;
            }

            returnStr += "}}";

            return returnStr;
        }

        private String FormatSettingsToJSON(String settingsXML)
        {
            StringBuilder returnString = new StringBuilder();
            StringReader xml = new StringReader(settingsXML);
            XmlReader pageSettings = XmlReader.Create(xml);

            while (pageSettings.Read())
                if (pageSettings.NodeType == XmlNodeType.Element)
                    switch (pageSettings.Name.ToLower())
                    {
                        case "template":
                            returnString.Append(AddTemplateObj(pageSettings));
                            pageSettings.Skip();
                            break;
                    }

            if (returnString.Length > 0)
                returnString.Remove(returnString.Length - 1, 1);

            return returnString.ToString();
        }
        private StringBuilder AddTemplateObj(XmlReader reader)
        {
            StringBuilder widgetsString = new StringBuilder("");
            StringBuilder returnString = new StringBuilder("{" + Environment.NewLine);
            returnString.Append("\"Name\" : \"" + (reader.GetAttribute("name")) + "\"," + Environment.NewLine);
            returnString.Append("\"Id\" : \"" + (reader.GetAttribute("id")) + "\"," + Environment.NewLine);
            returnString.Append("\"XMLTagName\" : \"template\"," + Environment.NewLine);
            returnString.Append("\"Children\" : [" + Environment.NewLine);

            while (reader.Read())
                if (reader.NodeType == XmlNodeType.Element)
                {
                    XmlReader subNodes = reader.ReadSubtree();

                    while (subNodes.Read())
                        if (subNodes.NodeType == XmlNodeType.Element)
                            switch (subNodes.Name)
                            {
                                case "template":
                                    returnString.Append(AddTemplateObj(subNodes));
                                    subNodes.Skip();
                                    break;
                                case "widget":
                                    returnString.Append(AddWidgetObj(subNodes));
                                    subNodes.Skip();
                                    break;
                            }
                }

            if (returnString.ToString().EndsWith(","))
                returnString.Remove(returnString.Length - 1, 1);

            returnString.Append("]");

            returnString.Append("},");
            return returnString;
        }
        private StringBuilder AddWidgetObj(XmlReader reader)
        {
            Boolean needTrim = false;
            StringBuilder returnString = new StringBuilder("{" + Environment.NewLine);
            returnString.Append("\"Name\" : \"" + (reader.GetAttribute("name")) + "\"," + Environment.NewLine);
            returnString.Append("\"Id\" : \"" + (reader.GetAttribute("id")) + "\"," + Environment.NewLine);
            returnString.Append("\"Type\" : \"" + (reader.GetAttribute("type")) + "\"," + Environment.NewLine);
            returnString.Append("\"XMLTagName\" : \"widget\"," + Environment.NewLine);
            returnString.Append("\"WidgetProperties\" : [" + Environment.NewLine);

            while (reader.Read())
                if (reader.NodeType == XmlNodeType.Element)
                {
                    XmlReader subNodes = reader.ReadSubtree();

                    while (subNodes.Read())
                        if (subNodes.NodeType == XmlNodeType.Element)
                        {
                            needTrim = true;
                            returnString.Append("{" + Environment.NewLine);
                            returnString.Append("\"Name\" : \"" + subNodes.Name + "\"," + Environment.NewLine);
                            returnString.Append("\"Value\" : \"" + subNodes.Value + "\"," + Environment.NewLine);
                            returnString.Append("\"ItemProperties\" : [" + Environment.NewLine);
                            returnString.Append(AddWidgetProperty(subNodes) + Environment.NewLine);
                            returnString.Append("]" + Environment.NewLine);
                            returnString.Append("},");
                        }
                }

            if (needTrim)
                returnString.Remove(returnString.Length - 1, 1);

            returnString.Append("]" + Environment.NewLine);
            returnString.Append("},");

            return returnString;
        }
        private StringBuilder AddWidgetProperty(XmlReader subNodes)
        {
            StringBuilder returnString = new StringBuilder();

            if (subNodes.HasAttributes)
            {
                returnString.Append("{");
                while (subNodes.MoveToNextAttribute())
                {
                    returnString.Append("\"" + subNodes.Name + "\" : \"" + subNodes.Value + "\",");
                }

                returnString.Remove(returnString.Length - 1, 1);
                subNodes.MoveToElement();
                returnString.Append("}" + Environment.NewLine);
            }

            return returnString;
        }
        private String GetDocTypes(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String returnString = "{\"docTypes\" : {" + Environment.NewLine;

            returnString += "\"type\" : [" + Environment.NewLine;
            foreach (String enumName in Enum.GetNames(typeof(BasePage.DocType)))
            {
                if (returnString.EndsWith("]"))
                {
                    returnString = returnString.Remove(returnString.Length - 1, 1);
                    returnString += ",";
                }

                returnString += "{" + Environment.NewLine;
                returnString += "\"enumNum\" : \"" + (int)Enum.Parse(typeof(BasePage.DocType), enumName) + "\",";
                returnString += "\"enumName\" : \"" + enumName + "\"";
                returnString += "}]";
            }

            returnString += "}}";

            return returnString;
        }
        private String AddNewWebsite(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String sql;
            String returnString = "";
            String updateString;
            String webSiteName = context.Request("url");
            String newID = Utilities.GenerateID(SaveType.WBST.ToString());

            sql = "INSERT INTO [OBiCMS].[dbo].[CMSWebsite] " +
                  "([CMSWebsiteID], [URL]) " +
                  "VALUES ('" + newID + "', '" + Utilities.ReplaceForDB(webSiteName) + "') ";

            updateString = Utilities.UpdateDatabase(sql);

            if (updateString == "true")
                returnString += "<newWebSiteID>" + newID + "</newWebSiteID>" + Environment.NewLine;
            else
                returnString += "<error>" + updateString + "</error>" + Environment.NewLine;

            return returnString;
        }
        private String SetupManager(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            DataSet ds = StaticObjects.GetDataset("Select [CMSWebsiteID], [URL] From CMSWebSite");
            StringBuilder returnString = new StringBuilder("");
            
            if (ds.Tables[0].Rows.Count > 0)
            {
                returnString.Append("{\"treeView\": {" + Environment.NewLine);

                returnString.Append("\"node\" : [" + Environment.NewLine);

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    returnString.Append("{" + Environment.NewLine +
                                        "\"type\" : \"site\"," + Environment.NewLine +
                                        "\"id\" : \"" + dr["CMSWebsiteID"].ToString() + "\"," + Environment.NewLine +
                                        "\"name\" : \"" + dr["URL"].ToString() + "\"," + Environment.NewLine);

                    String sql = "SELECT CMSBasePage.CMSBasePageID, CMSBasePage.Name " +
                                 "FROM CMSWebsite INNER JOIN " +
                                 "LK_CMSWebsite_URLRewriteJob ON CMSWebsite.CMSWebsiteIDC = LK_CMSWebsite_URLRewriteJob.CMSWebsiteIDC INNER JOIN " +
                                 "URLRewriteJob ON LK_CMSWebsite_URLRewriteJob.URLRewriteJobIDC = URLRewriteJob.URLRewriteJobIDC INNER JOIN " +
                                 "CMSBasePage ON URLRewriteJob.CMSBasePageIDC = CMSBasePage.CMSBasePageIDC " +
                                 "WHERE CMSWebsite.CMSWebsiteID = '" + dr["CMSWebsiteID"].ToString() + "' " +
                                 "ORDER BY CMSBasePage.Name";

                    DataSet basePages = StaticObjects.GetDataset(sql);

                    if (basePages.Tables[0].Rows.Count == 0)
                    {
                        returnString.Append("\"node\" : []},");
                        continue;
                    }

                    returnString.Append("\"node\" : [");
                    foreach (DataRow basePageDr in basePages.Tables[0].Rows)
                    {
                        returnString.Append("{" + Environment.NewLine +
                                            "\"type\" : \"basepage\"," + Environment.NewLine +
                                            "\"name\" : \"" + basePageDr["Name"].ToString() + "\"," + Environment.NewLine +
                                            "\"id\" : \"" + basePageDr["CMSBasePageID"].ToString() + "\"" + Environment.NewLine +
                                            "},");

                    }

                    if (returnString.ToString().EndsWith(","))
                        returnString.Remove(returnString.Length - 1, 1);

                    returnString.Append("]}," + Environment.NewLine);
                }

                returnString.Append(GetAllTemplates(context, pageSettings, widgetSettings, timer, useDiagnostics));
                returnString.Append(",");
                returnString.Append(GetAllCss(context, pageSettings, widgetSettings, timer, useDiagnostics));

                returnString.Append("]}}" + Environment.NewLine);
            }

            return returnString.ToString();
        }
        private StringBuilder LoadTemplates(BasePage.ContextManager context, BasePage.PageSettings pageSettings, String widgetSettings, Stopwatch timer, Boolean useDiagnostics)
        {
            String sql = "SELECT CMSTemplate.Name, CMSTemplateSettings.CMSTemplateSettingsID, CMSTemplateSettings.Body, CMSTemplateSettings.Javascript, " +
                         "CMSCSSSnippet.CMSCSSSnippetID, CMSCSSSnippet.Name AS CssName, CMSCSSSnippetSettings.CMSCSSSnippetSettingsID, " +
                         "CMSCSSSnippetSettings.Body AS CssBody " +
                         "FROM CMSTemplate INNER JOIN " +
                         "LK_CMSTemplate_CMSTemplateSettings ON CMSTemplate.CMSTemplateIDC = LK_CMSTemplate_CMSTemplateSettings.CMSTemplateIDC Left Join " +
                         "CMSTemplateSettings ON " +
                         "LK_CMSTemplate_CMSTemplateSettings.CMSTemplateSettingsIDC = CMSTemplateSettings.CMSTemplateSettingsIDC Left Join " +
                         "LK_CMSTemplateSettings_CMSCSSSnippet ON " +
                         "CMSTemplateSettings.CMSTemplateSettingsIDC = LK_CMSTemplateSettings_CMSCSSSnippet.CMSTemplateSettingstIDC Left Join " +
                         "CMSCSSSnippet ON LK_CMSTemplateSettings_CMSCSSSnippet.CMSCSSSnippetIDC = CMSCSSSnippet.CMSCSSSnippetIDC Left Join " +
                         "LK_CMSCSSSnippet_CMSCSSSnippetSettings ON " +
                         "CMSCSSSnippet.CMSCSSSnippetIDC = LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetIDC Left Join " +
                         "CMSCSSSnippetSettings ON " +
                         "LK_CMSCSSSnippet_CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC = CMSCSSSnippetSettings.CMSCSSSnippetSettingsIDC " +
                         "Where CMSTemplate.CMSTemplateID = '" + Utilities.ReplaceForDB(context.Request("templateID")) + "' AND CMSTemplateSettings.Online = 'True'";

            DataSet template = StaticObjects.GetDataset(sql);

            if (template.Tables[0].Rows.Count > 0)
            {
                DataRow dr = template.Tables[0].Rows[0];

                pageSettings.AddReplaceItem("[templateID]", context.Request("templateID"));
                pageSettings.AddReplaceItem("[settingsID]", dr["CMSTemplateSettingsID"].ToString());
                pageSettings.AddReplaceItem("[templateName]", dr["Name"].ToString());
                pageSettings.AddReplaceItem("[jsBody]", context.HtmlEncode(dr["Javascript"].ToString()));
                pageSettings.AddReplaceItem("[cssID]", dr["CMSCSSSnippetID"].ToString());
                pageSettings.AddReplaceItem("[cssSettingsID]", dr["CMSCSSSnippetSettingsID"].ToString());
                pageSettings.AddReplaceItem("[cssName]", dr["CssName"].ToString());
                pageSettings.AddReplaceItem("[cssBody]", context.HtmlEncode(dr["CssBody"].ToString()));
                pageSettings.AddReplaceItem("[htmlBody]", context.HtmlEncode(dr["Body"].ToString()));
            }
            else
            {
                pageSettings.AddReplaceItem("[templateID]", "");
                pageSettings.AddReplaceItem("[settingsID]", "");
                pageSettings.AddReplaceItem("[templateName]", "");
                pageSettings.AddReplaceItem("[jsBody]", "");
                pageSettings.AddReplaceItem("[cssID]", "");
                pageSettings.AddReplaceItem("[cssSettingsID]", "");
                pageSettings.AddReplaceItem("[cssName]", "");
                pageSettings.AddReplaceItem("[cssBody]", "");
                pageSettings.AddReplaceItem("[htmlBody]", "");
            }

            return new StringBuilder("");
        }

        // Protected Methods
        protected override void LoadWidget()
        {
        }
    }
}
